package com.earthea.scaffold.system.controller;

import com.earthea.scaffold.common.aop.annotation.ControllerLog;
import com.earthea.scaffold.common.entity.BaseCollectionRsp;
import com.earthea.scaffold.common.entity.BaseDataRsp;
import com.earthea.scaffold.common.entity.BaseRsp;
import com.earthea.scaffold.common.entity.PageResult;
import com.earthea.scaffold.common.enums.OperateTypeEnum;
import com.earthea.scaffold.common.util.ExcelUtil;
import com.earthea.scaffold.domain.DictType;
import com.earthea.scaffold.system.entity.request.DictTypeReq;
import com.earthea.scaffold.system.service.DictTypeService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 数据字典信息
 */
@RestController
@RequestMapping("/api/system/dict/type")
public class DictTypeController {
    @Autowired
    private DictTypeService dictTypeService;

    @GetMapping()
    public PageResult<DictType> list(DictTypeReq req) {
        return dictTypeService.selectPage(req);
    }

    @ControllerLog(title = "字典类型导出", operateType = OperateTypeEnum.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DictTypeReq req) {
        PageResult<DictType> select = dictTypeService.selectPage(req);
        ExcelUtil.export(response, "字典类型", select.getRows(), DictType.class);
    }

    /**
     * 查询字典类型详细
     */
    @GetMapping(value = "/{dictTypeId}")
    public BaseDataRsp<DictType> getInfo(@PathVariable("dictTypeId") Long dictTypeId) {
        return dictTypeService.selectById(dictTypeId);
    }

    /**
     * 刷新字典缓存
     */
    @ControllerLog(title = "字典类型刷新缓存", operateType = OperateTypeEnum.CLEAN)
    @DeleteMapping("/refreshCache")
    public BaseRsp refreshCache() {
        dictTypeService.resetDictCache();
        return BaseRsp.success();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/all")
    public BaseCollectionRsp<DictType> all() {
        return dictTypeService.selectAll();
    }
}
